package com.ztesoft.web.common.exchange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ztesoft.core.spring.context.SpringApplicationContextHolder;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.DateUtils;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.customdb.dao.BillingCycleCustomDao;
import com.ztesoft.web.common.customdb.dao.EnumListCustomDao;


/**
 * @Description 转换方法基础类
 * @author lin.weibin@zte.com.cn
 * @date 2015年8月6日 下午8:02:10
 * @since 1.0
 *
 */
@Service
public class ExchangeFunc {

	/**
	 * 日志
	 */
    private final static ZTEsoftLogManager LOGGER = ZTEsoftLogManager
            .getLogger(ExchangeFunc.class);
    
    @Autowired
    private EnumListCustomDao enumListCustomDao;
    
	/**
	 * 按本地网
	 * @param srcTablePo
	 * @param systemId
	 * @return
	 */
	public List<String> latnIdExchange(Integer systemId) {
		LOGGER.info("|按本地网方法转换|systemId="+systemId);
		return enumListCustomDao.selectByEnumCodeAndModuleId(DMSConstant.ENUM_CODE_LATN_ID, systemId);
	}
	
	/**
	 * 按清单序列
	 * @param srcTablePo
	 * @param systemId
	 * @return
	 */
	public List<String> detailSeqExchange(Integer systemId) {
		LOGGER.info("|按清单序列方法转换|systemId="+systemId);
		return enumListCustomDao.selectByEnumCodeAndModuleId(DMSConstant.ENUM_CODE_DETAIL_SEQ, systemId);
		
	}
	
	/**
	 * 按客户分组
	 * @param srcTablePo
	 * @param systemId
	 * @return
	 */
	public List<String> custCategoryExchange(Integer systemId) {
		LOGGER.info("|按客户分组方法转换|systemId="+systemId);
		return enumListCustomDao.selectByEnumCodeAndModuleId(DMSConstant.ENUM_CODE_CUST_CATEGORY_ID, systemId);
		
	}
	
	
	/**
	 * 按进程号分组
	 * @param srcTablePo
	 * @param systemId
	 * @return
	 */
	public List<String> pidExchange(Integer systemId) {
		LOGGER.info("|按进程号方法转换|systemId="+systemId);
		return enumListCustomDao.selectByEnumCodeAndModuleId(DMSConstant.ENUM_CODE__PID, systemId);
		
	}
	
	
	/**
	 * 按账期->往前偏移
	 * @param startOffset	偏移上限
	 * @param endOffset	偏移下限
	 * @return	账期列表
	 */
	public List<String> billingCycleExchange(Date date, Integer startOffset, Integer endOffset) {
		LOGGER.info("|当前时间按账期往前偏移|startOffset="+startOffset+",endOffset="+endOffset);
		List<String> list = new ArrayList<String>();
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		BillingCycleCustomDao billingCycleCustomDao = SpringApplicationContextHolder.getBean(BillingCycleCustomDao.class);
		List<String> yearMonthList = this.yearMonthExchange(date, startOffset, endOffset);
		if (!Utils.isEmpty(yearMonthList)) {
			for (String s : yearMonthList) {//
				list.addAll(billingCycleCustomDao.selectByCycleDate(s+"01"));
			}
			Utils.removeDuplicate(list);
		}
		LOGGER.info("|当前时间按账期往前偏移集|"+list);
		return list;
		
	}

	/**
	 * 将时间转成年月格式输出
	 * @param date
	 * @return
	 */
	public String billingCycleExchange(Date date){
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_MONTH_WITHOUT_SPLIT);
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		return df.format(date);
	}
	
	/**
	 * 按年月->往前偏移
	 * @param startOffset	偏移上限
	 * @param endOffset	偏移下限
	 * @return	年月值列表
	 */
	public List<String> yearMonthExchange(Date date, Integer startOffset, Integer endOffset) {
		LOGGER.info("|当前时间按年月往前偏移|startOffset="+startOffset+",endOffset="+endOffset);
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_MONTH_WITHOUT_SPLIT);
		List<String> list = new ArrayList<String>();
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		Date start = DateUtils.offersetMonth(date, -endOffset);
		Date end = DateUtils.offersetMonth(date, -startOffset);
		Calendar c = Calendar.getInstance();
		c.setTime(start);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(end);
		while(start.compareTo(end)<=0) {
			list.add(df.format(start));
			start = DateUtils.offersetMonth(start, 1);
		}
		LOGGER.info(""+list);
		return list;
		
	}



	/**
	 * 按年-月->往前偏移
	 * @param startOffset	偏移上限
	 * @param endOffset	偏移下限
	 * @return	年月值列表
	 */
	public List<String> yearMonthWithSplitExchange(Date date, Integer startOffset, Integer endOffset) {
		LOGGER.info("|当前时间按年月往前偏移|startOffset="+startOffset+",endOffset="+endOffset);
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_MONTH_WITH_SPLIT);
		List<String> list = new ArrayList<String>();
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		Date start = DateUtils.offersetMonth(date, -endOffset);
		Date end = DateUtils.offersetMonth(date, -startOffset);
		Calendar c = Calendar.getInstance();
		c.setTime(start);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(end);
		while(start.compareTo(end)<=0) {
			list.add(df.format(start));
			start = DateUtils.offersetMonth(start, 1);
		}
		LOGGER.info(""+list);
		return list;

	}



	/**
	 * 将时间转成年月格式输出
	 * @param date
	 * @return
	 */
	public String yearMonthExchange(Date date){
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_MONTH_WITHOUT_SPLIT);
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		return df.format(date);
	}
	
	/**
	 * 按年月日->往前偏移
	 * @param startOffset	偏移上限
	 * @param endOffset	偏移下限
	 * @return	年月日值列表
	 */
	public List<String> dateExchange(Date date, Integer startOffset, Integer endOffset) {
		LOGGER.info("|当前时间按年月日往前偏移|startOffset="+startOffset+",endOffset="+endOffset);
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_DAY_WITHOUT_SPLIT);
		List<String> list = new ArrayList<String>();
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		Date start = DateUtils.offsetDay(date, -endOffset);
		Date end = DateUtils.offsetDay(date, -startOffset);
		list.add(df.format(start));
		Calendar c = Calendar.getInstance();
		c.setTime(start);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(end);
		while(c1.after(c)) {
			c.add(Calendar.DATE, 1);
			list.add(df.format(c.getTime()));
		}
		LOGGER.info(""+list);
		return list;
	}




	/**
	 * 按年-月-日->往前偏移
	 * @param startOffset	偏移上限
	 * @param endOffset	偏移下限
	 * @return	年月日值列表
	 */
	public List<String> dateWithSplitExchange(Date date, Integer startOffset, Integer endOffset) {
		LOGGER.info("|当前时间按年月日往前偏移|startOffset="+startOffset+",endOffset="+endOffset);
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_DAY_WITH_SPLIT);
		List<String> list = new ArrayList<String>();
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		Date start = DateUtils.offsetDay(date, -endOffset);
		Date end = DateUtils.offsetDay(date, -startOffset);
		list.add(df.format(start));
		Calendar c = Calendar.getInstance();
		c.setTime(start);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(end);
		while(c1.after(c)) {
			c.add(Calendar.DATE, 1);
			list.add(df.format(c.getTime()));
		}
		LOGGER.info(""+list);
		return list;
	}

	/**
	 * 将时间转成年月日格式输出
	 * @param date
	 * @return
	 */
	public String dateExchange(Date date){
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_DAY_WITHOUT_SPLIT);
		if (Utils.isEmpty(date)) {
			date = DateUtils.getNowDate();
		}
		return df.format(date);
	}


	
	public static  String dateStrFormat(String str){
		DateFormat df = new SimpleDateFormat(DateUtils.STR_DEFAULT_DATE_FORMAT_WITH_SPLIT);
		return df.format(str);
	}
}
